package org.soenea.doitall.domain.model.invite.mapper;

import java.sql.SQLException;

import org.dsrg.soenea.domain.MapperException;
import org.dsrg.soenea.domain.mapper.GenericOutputMapper;
import org.dsrg.soenea.domain.mapper.LostUpdateException;
import org.soenea.doitall.domain.model.person.Person;
import org.soenea.doitall.domain.model.person.data.PersonTDG;
import org.soenea.doitall.domain.model.invite.*;
import org.soenea.doitall.domain.model.invite.data.InviteTDG;

public class InviteOutputMapper {

	public static void delete(Invite p) throws MapperException {
		try {
			final int count = InviteTDG.delete(p.getId(), p.getVersion());
			throwLostUpdateOnCountOfZero(p, count);
		} catch (final SQLException e) {
			throw new MapperException(e);
		}
	}
			
	public static void insert(Invite d) throws MapperException {
		try {
			InviteTDG.insert(d.getId(), d.getVersion(),d.getInviter().getId(),d.getGroup().getId(),d.getStatus().ordinal(), d.getInvited().getId());
		} catch (final SQLException e) {
			throw new MapperException(e);
		}
	}

	private static void throwLostUpdateOnCountOfZero(Invite p, int count)
			throws LostUpdateException {
		if (count == 0)
			throw new LostUpdateException("Invite: id = " + p.getId()
					+ "; version = " + p.getVersion() + " not found");
	}

	public static void update(Invite d) throws MapperException {
		try {
			final int count = InviteTDG.update(d.getId(), d.getVersion(),d.getInviter().getId(),d.getGroup().getId(),d.getStatus().ordinal(), d.getInvited().getId());
			throwLostUpdateOnCountOfZero(d, count);
			d.setVersion(d.getVersion() + 1);
		} catch (final SQLException e) {
			throw new MapperException(e);
		}
	}

}
